package top.leix.springdemo.services.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.leix.springdemo.NowSemester;
import top.leix.springdemo.Res;
import top.leix.springdemo.pojo.ApplyLabStudent;
import top.leix.springdemo.services.ApplyLabStudentService;
import top.leix.springdemo.mapper.ApplyLabStudentMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
* @author 16435
* @description 针对表【apply_lab_student(学生申请表)】的数据库操作Service实现
* @createDate 2024-04-21 22:23:42
*/
@Service
public class ApplyLabStudentServiceImpl extends ServiceImpl<ApplyLabStudentMapper, ApplyLabStudent>
    implements ApplyLabStudentService {
    @Resource
    ApplyLabStudentMapper applyLabStudentMapper;

    /**
     * 申请
     * @param applyLabStudent
     * @return
     */
    @Override
    public Res addApplication(ApplyLabStudent applyLabStudent) {
        applyLabStudent.setApplyStatus("Pending");
        applyLabStudent.setSemester(NowSemester.getNowSemester());
        int insertCount = applyLabStudentMapper.insert(applyLabStudent);
        if (insertCount > 0) {
            return Res.ok(null);
        } else {
            return Res.failed("添加申请失败");
        }
    }

    /**
     * 修改
     * @param applyLabStudent
     * @return
     */
    @Override
    public Res updateApplication(ApplyLabStudent applyLabStudent) {
        int updateCount = applyLabStudentMapper.updateById(applyLabStudent);
        if (updateCount > 0) {
            return Res.ok(null);
        } else {
            return Res.failed("更新申请失败");
        }
    }
    /**
     * 更新处理进度
     * @param status
     * @return
     */
    @Override
    public Res updateApplicationStatus(Integer applyID, String status) {
        int res = applyLabStudentMapper.updateStatus(applyID,status);
        if (res > 0){
            return Res.ok(null);
        }
        return Res.failed(null);
    }

    /**
     * 管理员查询所有学生表单
     */
    @Override
    public Res queryAllStudentApply() {
        List<ApplyLabStudent> labList = applyLabStudentMapper.selectList(new QueryWrapper<>());
        return Res.ok(labList);
    }
    /**
     * 管理员查询未审核表单
     */
    @Override
    public Res queryToCheckStudent() {
        QueryWrapper<ApplyLabStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Pending");
        queryWrapper.eq("semester", NowSemester.getNowSemester());
        List<ApplyLabStudent> labList = applyLabStudentMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    /**
     * 学生查询未审核
     * @param userAccount
     * @return
     */
    @Override
    public Res queryPending(String userAccount) {
        QueryWrapper<ApplyLabStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Pending");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyLabStudent> labList = applyLabStudentMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }
    /**
     * 学生查询已通过
     * @param userAccount 传入学生账户
     * @return 已通过列表
     */
    @Override
    public Res queryOK(String userAccount) {
        QueryWrapper<ApplyLabStudent> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("applyUserAccount",userAccount);
        queryWrapper.eq("applyStatus","OK");
        System.out.println(userAccount);
        List<ApplyLabStudent> labList = applyLabStudentMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }
    /**
     * 学生查询被拒绝
     * @param userAccount
     * @return
     */
    @Override
    public Res queryRejection(String userAccount) {
        QueryWrapper<ApplyLabStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Rejection");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyLabStudent> labList = applyLabStudentMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }
    /**
     * 学生查询历史
     * @param userAccount
     * @return
     */
    @Override
    public Res queryHistory(String userAccount) {
        QueryWrapper<ApplyLabStudent> queryWrapper = new QueryWrapper<>();
        //queryWrapper.isNotNull("applyID");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyLabStudent> labList = applyLabStudentMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    /**
     * 学生更新完成状态
     * @param applyID
     * @return
     */
    @Override
    public Res updateFinishedStatus(Integer applyID) {
        UpdateWrapper<ApplyLabStudent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("applyID", applyID) // 设置条件：applyID等于传入的applyID
                .set("applyStatus", "Finished"); // 设置applyStatus字段为Finished
        // 执行更新操作
        int result = this.baseMapper.update(null, updateWrapper);

        // 根据更新结果返回相应的响应
        if (result > 0) {
            return Res.ok(null);
        } else {
            return Res.failed(null);
        }
    }


}




